package Charts

import (
	"bytes"
	"golang.org/x/text/encoding/simplifiedchinese"
	"golang.org/x/text/transform"
	"io/ioutil"
)

func GbkToUtf8Str(s []byte) (string, error) {
	test, err := GbkToUtf8(s)
	if err != nil {
		return "", err
	}
	return string(test), nil
}

// gbk to utf8 encoding conversion
func GbkToUtf8(s []byte) ([]byte, error) {
	reader := transform.NewReader(bytes.NewReader(s), simplifiedchinese.GBK.NewDecoder())
	d, e := ioutil.ReadAll(reader)
	if e != nil {
		return nil, e
	}
	return d, nil
}

// utf8 to gbk encoding conversion
func Utf8ToGbk(s []byte) ([]byte, error) {
	reader := transform.NewReader(bytes.NewReader(s), simplifiedchinese.GBK.NewEncoder())
	d, e := ioutil.ReadAll(reader)
	if e != nil {
		return nil, e
	}
	return d, nil
}

// 字符串编码 gbk到utf8转换
func StrGbkToUtf8(str string) (string, error) {
	data, err := GbkToUtf8([]byte(str))
	return string(data), err
}

// 字符串编码 utf8到gbk转换
func StrUtf8ToGbk(str string) (string, error) {
	data, err := Utf8ToGbk([]byte(str))
	return string(data), err
}
